package com.xxxx.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxxx.server.pojo.Employee;
import com.xxxx.server.mapper.EmployeeMapper;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.pojo.RespPageBean;
import com.xxxx.server.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lsh
 * @since 2021-03-07
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 查询所有员工(分页)
     * @param currentPage
     * @param size
     * @param employee
     * @param beginDateScope
     * @return
     */
    @Override
    public RespPageBean getEmplByPage(Integer currentPage, Integer size, Employee employee, LocalDate[] beginDateScope) {
        /**
         * 开启分页
         * 使用com.baomidou.mybatisplus.extension.plugins.pagination.Page;
         */
        Page<Employee> page = new Page<>(currentPage,size);
        /**
         * 返回 IPage<> ，当中也有很多方法，使用getTotal()和getRecords()
         */
        IPage<Employee> emplByPage = employeeMapper.getEmplByPage(page, employee, beginDateScope);
        RespPageBean respPageBean = new RespPageBean(emplByPage.getTotal(), emplByPage.getRecords());
        return respPageBean;
    }

    /**
     * 获取最新工号：现获取当前最大工号 + 1
     * @return
     */
    @Override
    public RespBean maxWorkID() {
        List<Map<String, Object>> maps = employeeMapper.selectMaps(new QueryWrapper<Employee>().select("max(workID)"));
        //maps是object类型，不能直接从object转为int，先转为string，在转int，再+1。最后在转为string类型，添加格式化format以防乱码
        String workid = String.format("%08d", Integer.parseInt(maps.get(0).get("max(workID)").toString()) + 1);
        return RespBean.success(null,workid);
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @Override
    public RespBean addEmployee(Employee employee) {
        LocalDate beginContract = employee.getBeginContract();
        LocalDate endContract = employee.getEndContract();
        // 获取两个日期相差多少天，数据库中是按照年算，保留两位，天数/365
        long days = beginContract.until(endContract, ChronoUnit.DAYS);
        DecimalFormat decimalFormat = new DecimalFormat("##.00");
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(days/365.00)));
        // insert是int类型，表示的受影响行数
        if(1==employeeMapper.insert(employee)){
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }

    /**
     * 导出员工数据
     * 查询员工数据
     * @param id
     * @return
     */
    @Override
    public List<Employee> getEmplById(Integer id) {
        return employeeMapper.getEmplById(id);
    }
}
